﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;

namespace Project
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            if(IsDataBaseInstalled())
                HideMenuAndShowLogin();
            else this.Loaded +=DataBaseFailed; 
        }

        private void DataBaseFailed(object sender, RoutedEventArgs routedEventArgs)
        {
            Xceed.Wpf.Toolkit.MessageBox.Show("Error: La base de datos no esta instalada en el equipo, esto puede deberse a una mala instalación, porfavor reinstale el programa","Error!",MessageBoxButton.OK,MessageBoxImage.Error);
            this.Close();
        }

        private bool IsDataBaseInstalled()
        {
            const string server = ".\\sqlexpress";
            const string database = "brigada";
            const string connString = ("Data Source=" + (server + ";Initial Catalog=master;Integrated Security=True;"));
            String cmdText = ("select * from master.dbo.sysdatabases where name=\'" + (database + "\'"));
            Boolean bRet;

            System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connString);
            System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(cmdText, sqlConnection);
            
            try
            {
                sqlConnection.Open();
                System.Data.SqlClient.SqlDataReader reader = sqlCmd.ExecuteReader();
                bRet = reader.HasRows;
                sqlConnection.Close();
            }
            catch (Exception e) 
            {
                bRet = false;
                sqlConnection.Close();
                MessageBox.Show(e.Message);
            } //End Try Catch Block

            return bRet;
        }

        private void HideMenuAndShowLogin()
        {
            AddHandler(Keyboard.KeyDownEvent,(KeyEventHandler)KeyPressed);
            if (UserControllerAndDataAccess.IsUserLogged())
            {
                gridLogin.Visibility = Visibility.Hidden;
                MainMenuControl.Visibility = Visibility.Visible;
                im1.Visibility = Visibility.Hidden;
                im2.Visibility = Visibility.Hidden;
                title.Visibility = Visibility.Hidden;
                btnLogOut.Visibility = Visibility.Visible;
                lblRights.Visibility = Visibility.Hidden;
                ;
            }
            else
            {
                gridLogin.Visibility = Visibility.Visible;
                MainMenuControl.Visibility = Visibility.Collapsed;
                btnLogOut.Visibility = Visibility.Collapsed;
                im1.Visibility = Visibility.Visible;
                im2.Visibility = Visibility.Visible;
                title.Visibility = Visibility.Visible;
                lblRights.Visibility = Visibility.Visible;
                this.Content.Children.Clear();
                this.Content.Children.Add(LogThings);
            }
        }
        private void KeyPressed(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (this.Content.FindName("btnLogIn")!=null && btnLogIn.IsVisible)
                    btnLogIn_Click(null, null);
            }
        }

        private void DirectAction_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new DirectActionRegister());
        }

        private void FunctionaryRegister_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new FunctionaryRegister());
        }

        private void RegisterComplaint_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new RegisterComplaint());
        }

        private void FindFunctionary_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new AllFunctionaries());
        }

        private void AllComplaints_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new AllComplaints());
        }

        private void RegisterAudience_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new RegisterAudience());
        }

        private void RequestingMedical_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new RequestingMedical());
        }

        private void PsicologistReport_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new RegisterPsicologistReport());
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new RegisterCitation());
        }


        private void NewDeclaration_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new RegisterDeclaration());
        }


        private void Statistics_Click(object sender, RoutedEventArgs e)
        {
            Content.Children.Clear();
            Content.Children.Add(new StatisticalReports());
;        }

        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            lblLoginError.Visibility = Visibility.Hidden;
            bool resp = UserControllerAndDataAccess.LogInUser(tbUsername.Text, tbPassword.Password);
            if (!resp)
            {
                tbPassword.Password = null;
                lblLoginError.Visibility = Visibility.Visible;
            }
            else
            {
                btnLogOut.Visibility = Visibility.Visible;
                lblLoginError.Visibility = Visibility.Hidden;
                MainMenuControl.Visibility = Visibility.Visible;

                if (UserControllerAndDataAccess.IsDirectorLogged)
                    SetDirectorMenu();
                else
                {
                    if (UserControllerAndDataAccess.IsPsicologistLogged)
                        SetPsicologistMenu();
                    else
                        SetEmployeeMenu();
                }
                Content.Children.Clear();
                var allComplaints = new AllComplaints();
                HideMenuAndShowLogin();
                Xceed.Wpf.Toolkit.MessageBox.Show("Usuario autenticado con exito!, Bienvenido(a)...", "Exito!", MessageBoxButton.OK,
                                                 MessageBoxImage.Information);
                Content.Children.Add(allComplaints);
            }
        }

        private void SetEmployeeMenu()
        {
            //UNDO PREVIOUS MENU HIDES
            RegisterComplaintMenu.Visibility = Visibility.Visible;
            RegisterFunctionalityMenu.Visibility = Visibility.Visible;
            StatisticsMenu.Visibility = Visibility.Visible;
            EmployeesMenu.Visibility = Visibility.Visible;
            Action.Visibility = Visibility.Visible;
            Medica.Visibility = Visibility.Visible;
            Citacion.Visibility = Visibility.Visible;
            Audiencia.Visibility = Visibility.Visible;
            Declaracion.Visibility = Visibility.Visible;
            RegisterComplaintMenu.Visibility = Visibility.Visible;
            //HIDE RESPECTIVELY
            StatisticsMenu.Visibility = Visibility.Collapsed;
            EmployeesMenu.Visibility = Visibility.Collapsed;
            PsicoReport.Visibility = Visibility.Collapsed;
        }

        private void SetPsicologistMenu()
        {
            //UNDO PREVIOUS MENU HIDES
            StatisticsMenu.Visibility = Visibility.Visible;
            EmployeesMenu.Visibility = Visibility.Visible;
            PsicoReport.Visibility = Visibility.Visible;
            RegisterComplaintMenu.Visibility = Visibility.Visible;
            RegisterFunctionalityMenu.Visibility = Visibility.Visible;
            //HIDE RESPECTIVELY
            StatisticsMenu.Visibility = Visibility.Collapsed;
            EmployeesMenu.Visibility = Visibility.Collapsed;
            Action.Visibility = Visibility.Collapsed;
            Medica.Visibility = Visibility.Collapsed;
            Citacion.Visibility = Visibility.Collapsed;
            Audiencia.Visibility = Visibility.Collapsed;
            Declaracion.Visibility = Visibility.Collapsed; 
            RegisterComplaintMenu.Visibility = Visibility.Collapsed;
        }

        private void SetDirectorMenu()
        {
            //UNDO PREVIOUS MENU HIDES
            StatisticsMenu.Visibility = Visibility.Visible;
            EmployeesMenu.Visibility = Visibility.Visible;
            PsicoReport.Visibility = Visibility.Visible;
            StatisticsMenu.Visibility = Visibility.Visible;
            EmployeesMenu.Visibility = Visibility.Visible;
            Action.Visibility = Visibility.Visible;
            Medica.Visibility = Visibility.Visible;
            Citacion.Visibility = Visibility.Visible;
            Audiencia.Visibility = Visibility.Visible;
            Declaracion.Visibility = Visibility.Visible;
            RegisterComplaintMenu.Visibility = Visibility.Visible;
            //HIDE RESPECTIVELY
            RegisterComplaintMenu.Visibility = Visibility.Collapsed;
            RegisterFunctionalityMenu.Visibility = Visibility.Collapsed;
        }

        private void btnLogOut_Click(object sender, RoutedEventArgs e)
        {
            var style = new Style(typeof(Xceed.Wpf.Toolkit.MessageBox));
            style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.CancelButtonContentProperty, "Cancelar"));
            style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.OkButtonContentProperty, "Aceptar"));
            var res = Xceed.Wpf.Toolkit.MessageBox.Show("Usted esta a punto de cerrar su sesión! tendra que volver a iniciarla para usar el programa", "Confirmar Accion!", MessageBoxButton.OKCancel, MessageBoxImage.Warning, style);
            if (res == MessageBoxResult.OK)
            {
                tbUsername.Text = null;
                tbPassword.Password = null;
                UserControllerAndDataAccess.LogOutCurrentUser();
                HideMenuAndShowLogin();
            }
        }
    }
}
